﻿// MyDialog.cpp: 实现文件
//

#include "pch.h"
#include "MFCApplication1.h"
#include "MyDialog.h"
#include "afxdialogex.h"
#include "MFCApplication1Doc.h"

// MyDialog 对话框

IMPLEMENT_DYNAMIC(MyDialog, CDialogEx)

MyDialog::MyDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
	, m_name(_T(""))
	, m_tel(_T(""))
	, m_weight(70)
	, m_height(180)
	, m_sex(false)
	, m_grade(_T(""))
{
#ifndef _WIN32_WCE
	EnableActiveAccessibility();
#endif
}
//重写虚函数，控件初始化
BOOL MyDialog::OnInitDialog() {

		CDialog::OnInitDialog();
		this->MoveWindow(500, 200, 630, 500);
		//设置性别，默认选择“男”
		((CButton*)GetDlgItem(IDC_SEX_MALE))->SetCheck(TRUE);
		//GetDlgItem(IDC_PICTURE)->MoveWindow(200, 500, 50, 50, true);
		CRect rect;
		//设置图片控件的位置、大小
		GetDlgItem(IDC_PICTURE)->GetWindowRect(&rect);           //IDC_WAVE_DRAW为Picture Control的ID
		ScreenToClient(&rect);
		GetDlgItem(IDC_PICTURE)->MoveWindow(rect.left, rect.top, 100, 140, true);
		
		//身高spin控件，设置范围、跨度、关联编辑框、位置
		spin_height.SetRange(140, 200);
		spin_height.SetBase(60);
		spin_height.SetBuddy(GetDlgItem(IDC_EDIT_HEIGHT));
		spin_height.SetPos(180);
		//体重slider控件，设置范围、刻度、位置
		slider_weight.SetRange(40, 100);
		slider_weight.SetTicFreq(1);
		slider_weight.SetPos(70);
		//初始化
		m_sex = false;
		m_name = "请输入姓名";
		m_height = 180;
		m_weight = 70;
		m_tel = "请输入电话";
		//false是将控件value传递给控件，更新
		UpdateData(false);
	return true;
}
MyDialog::~MyDialog()
{
}

void MyDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_NAME, edit_name);
	DDX_Control(pDX, IDC_EDIT_TELE, edit_tele);
	DDX_Text(pDX, IDC_EDIT_NAME, m_name);
	DDX_Text(pDX, IDC_EDIT_TELE, m_tel);
	DDX_Control(pDX, IDC_EDIT_HEIGHT, edit_height);
	DDX_Control(pDX, IDC_EDIT_WEIGHT, edit_weight);
	DDX_Text(pDX, IDC_EDIT_WEIGHT, m_weight);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_height);
	DDX_Control(pDX, IDC_GRADE, combo_grade);
	DDX_CBString(pDX, IDC_GRADE, m_grade);
	DDX_Control(pDX, IDC_SPIN_HEIGHT, spin_height);
	DDX_Control(pDX, IDC_SLIDER_WEIGHT, slider_weight);
	DDX_Control(pDX, IDC_BIRTHDAY, date_birthday);
}


BEGIN_MESSAGE_MAP(MyDialog, CDialogEx)
	ON_WM_HSCROLL()
	ON_STN_CLICKED(IDC_PICTURE, &MyDialog::OnStnClickedPicture)
	ON_BN_CLICKED(IDC_LOAD, &MyDialog::OnBnClickedLoad)
	ON_EN_CHANGE(IDC_EDIT_NAME, &MyDialog::OnEnChangeEditName)
	ON_EN_CHANGE(IDC_EDIT_TELE, &MyDialog::OnEnChangeEditTele)
	ON_EN_CHANGE(IDC_EDIT_HEIGHT, &MyDialog::OnEnChangeEditHeight)
END_MESSAGE_MAP()


// MyDialog 消息处理程序


void MyDialog::OnOK()
{
	CMFCApplication1Doc* pDoc = (CMFCApplication1Doc*)(GetParentFrame()->GetActiveDocument());
	CString str;
	//从姓名编辑框获取姓名
	edit_name.GetWindowTextW(str);
	pDoc->m_name = str;
	//获取性别
	pDoc->m_sex = ((CButton*)GetDlgItem(IDC_SEX_FEMALE))->GetCheck();
	//获取年级(注意坐标是从0开始的，要加1)
	pDoc->m_grade = combo_grade.GetCurSel() + 1;
	MessageBox(L"你好,"+pDoc->m_name);
	//获取电话
	edit_tele.GetWindowTextW(str);
	pDoc->m_tel = str;
	//判断喜好，用整型变量的3个bit存储
	if (((CButton*)GetDlgItem(IDC_LIKE_FOOT))->GetCheck())
		pDoc->m_like = 4;
	else
		pDoc->m_like = 0;
	if (((CButton*)GetDlgItem(IDC_LIKE_BASKET))->GetCheck())
		pDoc->m_like += 2;
	if (((CButton*)GetDlgItem(IDC_LIKE_VOLLY))->GetCheck())
		pDoc->m_like += 1;
	//获取身高
	edit_height.GetWindowTextW(str);
	pDoc->m_height = _wtoi(str);
	//获取体重
	edit_weight.GetWindowTextW(str);
	pDoc->m_weight = _wtoi(str);
	//获取日期，并以字符串形式存储年月日
	date_birthday.GetTime(pDoc->m_birthday);
	pDoc->m_birthday_str.Format(L"%d",pDoc->m_birthday.GetYear());
	pDoc->m_birthday_str2.Format(L"%d", pDoc->m_birthday.GetMonth());
	pDoc->m_birthday_str3.Format(L"%d", pDoc->m_birthday.GetDay());
	//加上此变量是为了，第一次打开时不会显示日期
	pDoc->m_datedisplay = true;
	//获取位图路径
	pDoc->m_bmp = this->m_bmp;
	//父窗口视图更新
	GetParentFrame()->GetActiveView()->Invalidate(true);
	CDialogEx::OnOK();
}







void MyDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	//slider滑动动态更新编辑框的value
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
	CWnd* slider = GetDlgItem(IDC_SLIDER_WEIGHT);
	m_weight = 1 * ((CSliderCtrl*)slider)->GetPos();
	UpdateData(FALSE);
}


void MyDialog::OnStnClickedPicture()
{

}


void MyDialog::OnBnClickedLoad()
{
	UpdateData(false);
	//过滤器
	CString filter;
	filter = "所有文件(*.bmp,*.jpg,*.gif,*tiff)|*.bmp;*.jpg;*.gif;*.tiff| BMP(*.bmp)|*.bmp| JPG(*.jpg)|*.jpg| GIF(*.gif)|*.gif| TIFF(*.tiff)|*.tiff||";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter, NULL);

	if (dlg.DoModal() == IDOK) {
		//文件名（路径）、拓展名
		CString BmpName = dlg.GetPathName();
		CString EntName = dlg.GetFileExt();
		m_bmp = BmpName;
		EntName.MakeLower();

		if (EntName.Compare(_T("bmp")) == 0) {
			BITMAPINFO* pBmpInfo;       //记录图像细节
			BYTE* pBmpData;             //图像数据
			BITMAPFILEHEADER bmpHeader; //文件头
			BITMAPINFOHEADER bmpInfo;   //信息头
			CFile bmpFile;              //记录打开文件

			//以只读的方式打开文件 读取bmp图片各部分 bmp文件头 信息 数据
			if (!bmpFile.Open(BmpName, CFile::modeRead | CFile::typeBinary))
				return;
			if (bmpFile.Read(&bmpHeader, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
				return;
			if (bmpFile.Read(&bmpInfo, sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
				return;
			pBmpInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
			//为图像数据申请空间
			memcpy(pBmpInfo, &bmpInfo, sizeof(BITMAPINFOHEADER));
			DWORD dataBytes = bmpHeader.bfSize - bmpHeader.bfOffBits;
			pBmpData = (BYTE*)new char[dataBytes];
			bmpFile.Read(pBmpData, dataBytes);
			bmpFile.Close();

			//显示图像
			/*CWnd* pWnd = GetDlgItem(IDC_PICTURE); //获得pictrue控件窗口的句柄
			CRect rect;
			pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域
			CDC* pDC = pWnd->GetDC(); //获得pictrue控件的DC
			pDC->SetStretchBltMode(COLORONCOLOR);
			//pDC->StretchBlt(0, 0, 400, 300, pDC, 0, 0, bmpInfo.biWidth, bmpInfo.biHeight, SRCCOPY);
			StretchDIBits(pDC->GetSafeHdc(), 0, 0, rect.Width(), rect.Height(), 100, 100,
			bmpInfo.biWidth, bmpInfo.biHeight, pBmpData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
		*/
			//显示图像
			CStatic* pwnd = (CStatic*)GetDlgItem(IDC_PICTURE);  //里面的参数是图片控件的ID
			//pwnd->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
			pwnd->ModifyStyle(0xf, SS_BITMAP);
			pwnd->SetBitmap((HBITMAP)::LoadImage(GetModuleHandle(NULL), BmpName, IMAGE_BITMAP, 100, 140, LR_LOADFROMFILE));
		}
	}
}


void MyDialog::OnEnChangeEditName()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
	edit_name.GetWindowTextW(m_name);
	// TODO:  在此添加控件通知处理程序代码
}


void MyDialog::OnEnChangeEditTele()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
	edit_tele.GetWindowTextW(m_tel);
	// TODO:  在此添加控件通知处理程序代码
}


void MyDialog::OnEnChangeEditHeight()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
	CString str;
	edit_height.GetWindowTextW(str);
	m_height = _wtoi(str);
	// TODO:  在此添加控件通知处理程序代码
}
